import {createStore} from 'vuex'

let userInfo = {};
let userToken = {};

if (localStorage.userInfo) {
    userInfo = JSON.parse(localStorage.userInfo);
}
if (localStorage.userToken) {
    userToken = JSON.parse(localStorage.userToken);
}

export default createStore({
    state: {
        userInfo,
        userToken,
        tagsList: [],
        collapse: false,
        isRefreshing: false,
        retryQueue: [],
        isShowMsg: false,
    },
    mutations: {
        delTagsItem(state, data) {
            state.tagsList.splice(data.index, 1);
        },
        setTagsItem(state, data) {
            state.tagsList.push(data)
        },
        clearTags(state) {
            state.tagsList = []
        },
        closeTagsOther(state, data) {
            state.tagsList = data;
        },
        closeCurrentTag(state, data) {
            for (let i = 0, len = state.tagsList.length; i < len; i++) {
                const item = state.tagsList[i];
                if (item.path === data.$route.fullPath) {
                    if (i < len - 1) {
                        data
                            .$router
                            .push(state.tagsList[i + 1].path);
                    } else if (i > 0) {
                        data
                            .$router
                            .push(state.tagsList[i - 1].path);
                    } else {
                        data
                            .$router
                            .push("/");
                    }
                    state
                        .tagsList
                        .splice(i, 1);
                    break;
                }
            }
        },
        // 侧边栏折叠
        hadndleCollapse(state, data) {
            state.collapse = data;
        },
        // 设置账号基本信息
        setUserInfo(state, data) {
            localStorage.userInfo = JSON.stringify(data);
            state.userInfo = data;
        },
        // 清空账号信息
        clearUserInfo(state) {
            state.collapse = false;
            state.userInfo = {};
            localStorage.removeItem('userInfo');
        },
        // 设置token信息
        setUserToken(state, data) {
            localStorage.userToken = JSON.stringify(data);
            state.userToken = data;
        },
        // 清空token信息
        clearUserToken(state) {
            state.collapse = false;
            state.userToken = {};
            localStorage.removeItem('userToken');
        },
        setIsRefreshing(state, value) {
            state.isRefreshing = value;
        },
        addRetryRequest(state, cb) {
            state.retryQueue.push(cb);
        },
        clearRetryQueue(state) {
            state.retryQueue = [];
        },
        setIsShowMsg(state, value) {
            state.isShowMsg = value;
        },
    },
    actions: {},
    getters: {
        getUserInfo(state) {
            return state.userInfo
        },
        getUserToken(state) {
            return state.userToken
        }
    },
    modules: {}
})
